package com.noshufou.android.su.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.widget.Toast;
import com.noshufou.android.su.HomeActivity;
import com.noshufou.android.su.provider.PermissionsProvider.Apps;
import com.noshufou.android.su.provider.PermissionsProvider.Logs;
import com.noshufou.android.su.util.Util;

public class ResultService extends IntentService
{
  public static final int ACTION_RECYCLE = 2;
  public static final int ACTION_RESULT = 1;
  private static final int COLUMN_ALLOW = 1;
  private static final int COLUMN_ID = 0;
  private static final int COLUMN_LOGGING = 3;
  private static final int COLUMN_NOTIFICATIONS = 2;
  public static final String EXTRA_ACTION = "action";
  public static final String[] PROJECTION;
  private static final String TAG = "Su.ResultService";
  final String LAST_NOTIFICATION_TIME = "last_notification_time";
  final String LAST_NOTIFICATION_UID = "last_notification_uid";
  private Handler mHandler;
  private boolean mLog = true;
  private boolean mNotify = true;
  private String mNotifyType = "toast";
  private SharedPreferences mPrefs = null;

  static
  {
    String[] arrayOfString = new String[4];
    arrayOfString[0] = "_id";
    arrayOfString[1] = "allow";
    arrayOfString[2] = "notifications";
    arrayOfString[3] = "logging";
    PROJECTION = arrayOfString;
  }

  public ResultService()
  {
    super("Su.ResultService");
  }

  private void addLog(long paramLong1, int paramInt1, int paramInt2, String paramString1, int paramInt3, long paramLong2, String paramString2)
  {
    if (((paramString2 == null) && (!this.mLog)) || ((paramString2 != null) && (paramString2.equals("0"))) || (paramInt3 == -1));
    while (true)
    {
      return;
      ContentValues localContentValues = new ContentValues();
      if (paramLong1 == -1L)
      {
        localContentValues.put("uid", Integer.valueOf(paramInt1));
        localContentValues.put("package", Util.getAppPackage(this, paramInt1));
        localContentValues.put("name", Util.getAppName(this, paramInt1, false));
        localContentValues.put("exec_uid", Integer.valueOf(paramInt2));
        localContentValues.put("exec_cmd", paramString1);
        localContentValues.put("allow", Integer.valueOf(-1));
        paramLong1 = Long.parseLong(getContentResolver().insert(PermissionsProvider.Apps.CONTENT_URI, localContentValues).getLastPathSegment());
      }
      localContentValues.clear();
      localContentValues.put("date", Long.valueOf(paramLong2));
      localContentValues.put("type", Integer.valueOf(paramInt3));
      getContentResolver().insert(Uri.withAppendedPath(PermissionsProvider.Logs.CONTENT_URI, String.valueOf(paramLong1)), localContentValues);
    }
  }

  private void ensurePrefs()
  {
    if (this.mPrefs == null)
    {
      this.mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
      this.mNotify = this.mPrefs.getBoolean("pref_notifications", true);
      this.mNotifyType = this.mPrefs.getString("pref_notification_type", "toast");
      this.mLog = this.mPrefs.getBoolean("pref_logging", true);
    }
  }

  private void recycle()
  {
    ensurePrefs();
    if (!this.mPrefs.getBoolean("pref_delete_old_logs", true));
    while (true)
    {
      return;
      int i = this.mPrefs.getInt("pref_log_entry_limit", 200);
      ContentResolver localContentResolver1 = getContentResolver();
      Uri localUri1 = PermissionsProvider.Logs.CONTENT_URI;
      String[] arrayOfString1 = new String[1];
      arrayOfString1[0] = "COUNT() as rows";
      Cursor localCursor1 = localContentResolver1.query(localUri1, arrayOfString1, null, null, null);
      localCursor1.moveToFirst();
      int j = localCursor1.getInt(0);
      localCursor1.close();
      if (j <= i)
        continue;
      ContentResolver localContentResolver2 = getContentResolver();
      Uri localUri2 = PermissionsProvider.Logs.CONTENT_URI;
      String[] arrayOfString2 = new String[1];
      arrayOfString2[0] = "_id";
      Cursor localCursor2 = localContentResolver2.query(localUri2, arrayOfString2, null, null, "date ASC");
      while ((j > i) && (localCursor2.moveToNext()))
      {
        long l = localCursor2.getLong(0);
        ContentResolver localContentResolver3 = getContentResolver();
        Uri localUri3 = PermissionsProvider.Logs.CONTENT_URI;
        String[] arrayOfString3 = new String[1];
        arrayOfString3[0] = String.valueOf(l);
        j -= localContentResolver3.delete(localUri3, "_id=?", arrayOfString3);
      }
    }
  }

  private void sendNotification(long paramLong1, int paramInt1, int paramInt2, long paramLong2, String paramString)
  {
    if (((paramString == null) && (!this.mNotify)) || ((paramString != null) && (paramString.equals("0"))) || (paramInt2 == -1));
    while (true)
    {
      return;
      if (paramInt2 == 1);
      String str;
      for (int i = 2131427502; ; i = 2131427503)
      {
        Object[] arrayOfObject = new Object[1];
        arrayOfObject[0] = Util.getAppName(this, paramInt1, false);
        str = getString(i, arrayOfObject);
        if (!this.mNotifyType.equals("toast"))
          break label224;
        ensurePrefs();
        int j = this.mPrefs.getInt("last_notification_uid", 0);
        long l = this.mPrefs.getLong("last_notification_time", 0L);
        int k = Integer.parseInt(this.mPrefs.getString("pref_toast_location", "0"));
        if ((j == paramInt1) && (5000L + l >= paramLong2))
          break;
        this.mHandler.post(new Runnable(str, k)
        {
          public void run()
          {
            Toast localToast = Toast.makeText(ResultService.this.getApplicationContext(), this.val$notificationMessage, 0);
            if (this.val$gravity > 0)
              localToast.setGravity(this.val$gravity, 0, 0);
            localToast.show();
          }
        });
        SharedPreferences.Editor localEditor = this.mPrefs.edit();
        localEditor.putInt("last_notification_uid", paramInt1);
        localEditor.putLong("last_notification_time", paramLong2);
        localEditor.commit();
        break;
      }
      label224: if (!this.mNotifyType.equals("status"))
        continue;
      NotificationManager localNotificationManager = (NotificationManager)getSystemService("notification");
      PendingIntent localPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, HomeActivity.class), 0);
      Notification localNotification = new Notification(2130837556, str, paramLong2);
      localNotification.setLatestEventInfo(this, getString(2131427328), str, localPendingIntent);
      localNotification.flags = (0x18 | localNotification.flags);
      localNotificationManager.notify(paramInt1, localNotification);
    }
  }

  public void onCreate()
  {
    super.onCreate();
    this.mHandler = new Handler();
  }

  protected void onHandleIntent(Intent paramIntent)
  {
    int i;
    int j;
    long l1;
    String str1;
    String str2;
    Cursor localCursor;
    long l2;
    int k;
    switch (paramIntent.getIntExtra("action", 0))
    {
    default:
      throw new IllegalArgumentException();
    case 1:
      ensurePrefs();
      i = paramIntent.getIntExtra("caller_uid", 0);
      j = paramIntent.getIntExtra("allow", -1);
      l1 = System.currentTimeMillis();
      str1 = null;
      str2 = null;
      localCursor = getContentResolver().query(Uri.withAppendedPath(PermissionsProvider.Apps.CONTENT_URI, "uid/" + i), PROJECTION, null, null, null);
      if ((localCursor != null) && (localCursor.moveToFirst()))
      {
        l2 = localCursor.getLong(0);
        str1 = localCursor.getString(2);
        str2 = localCursor.getString(3);
        int n = localCursor.getInt(1);
        if (n == -1)
          break;
        k = n;
      }
    case 2:
    }
    while (true)
    {
      localCursor.close();
      sendNotification(l2, i, k, l1, str1);
      int m = paramIntent.getIntExtra("desired_uid", 0);
      String str3 = paramIntent.getStringExtra("desired_cmd");
      addLog(l2, i, m, str3, k, l1, str2);
      recycle();
      return;
      k = j;
      continue;
      l2 = -1L;
      k = j;
    }
  }
}

/* Location:           D:\Development\Decompile_Workspace\슈퍼유저-41\classes_dex2jar.jar
 * Qualified Name:     com.noshufou.android.su.service.ResultService
 * JD-Core Version:    0.6.0
 */